#include "ticket.hpp"
#include "mtx.hpp"
#include <time.h>
#include <unistd.h>
ticket a;
pthread_mutex_t sssssss = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t wwait = PTHREAD_COND_INITIALIZER;
void *enter(void *args)
{
    int *p = (int *)args;
    sleep(1);
    cout << "我是线程" << pthread_self() << "   " << (*p) << "    " << p << endl;
    while (true)
    {
        sleep(1);
        a.buyticket(*p, wwait);
        pthread_mutex_lock(&sssssss);
        pthread_cond_wait(&wwait, &sssssss);
        pthread_mutex_unlock(&sssssss);
    }
    delete p;
    return nullptr;
}
int main()
{
    pthread_t tid[4];
    /*int p ;*/
    for (int i = 0; i < 4; i++)
    {
        int *p = new int(i + 1);
        cout << p << endl;
        pthread_create(tid + i, nullptr, enter, p);
        sleep(1);
    }
    // while (true)
    // {
    //     a.date();
    // }
    for (int i = 0; i < 4; i++)
    {
        cout << tid[i] << endl;
        pthread_join(tid[i], nullptr);
    }
    cout << "回收成功" << endl;
    return 0;
}